﻿package org.papervision3d.cameras
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.ui.*;
    import org.papervision3d.view.*;

    public class DebugCamera3D extends Camera3D
    {
        protected var keyLeft:Boolean = false;
        protected var targetRotationX:Number = 0;
        protected var targetRotationY:Number = 0;
        protected var sideFactor:Number = 0;
        protected var _propertiesDisplay:Sprite;
        protected var viewport3D:Viewport3D;
        protected var fovText:TextField;
        protected var xText:TextField;
        protected var yText:TextField;
        protected var zText:TextField;
        protected var startPoint:Point;
        protected var startRotationX:Number;
        protected var startRotationY:Number;
        protected var keyBackward:Boolean = false;
        protected var farText:TextField;
        protected var keyForward:Boolean = false;
        protected var rotationXText:TextField;
        protected var rotationZText:TextField;
        protected var rotationYText:TextField;
        protected var forwardFactor:Number = 0;
        protected var nearText:TextField;
        protected var viewportStage:Stage;
        protected var _inertia:Number = 3;
        protected var keyRight:Boolean = false;

        public function DebugCamera3D(param1:Viewport3D, param2:Number = 90, param3:Number = 10, param4:Number = 5000)
        {
            super(param2, param3, param4, true);
            this.viewport3D = param1;
            this.viewport = param1.sizeRectangle;
            this.focus = this.viewport.height / 2 / Math.tan(param2 / 2 * (Math.PI / 180));
            this.zoom = this.focus / param3;
            this.focus = param3;
            this.far = param4;
            this.displayProperties();
            this.checkStageReady();
            return;
        }// end function

        protected function keyDownHandler(param1:KeyboardEvent) : void
        {
            switch(param1.keyCode)
            {
                case "W".charCodeAt():
                case Keyboard.UP:
                {
                    this.keyForward = true;
                    this.keyBackward = false;
                    break;
                }// end case
                case "S".charCodeAt():
                case Keyboard.DOWN:
                {
                    this.keyBackward = true;
                    this.keyForward = false;
                    break;
                }// end case
                case "A".charCodeAt():
                case Keyboard.LEFT:
                {
                    this.keyLeft = true;
                    this.keyRight = false;
                    break;
                }// end case
                case "D".charCodeAt():
                case Keyboard.RIGHT:
                {
                    this.keyRight = true;
                    this.keyLeft = false;
                    break;
                }// end case
                case "Q".charCodeAt():
                {
                    rotationZ--;
                    break;
                }// end case
                case "E".charCodeAt():
                {
                    rotationZ++;
                    break;
                }// end case
                case "F".charCodeAt():
                {
                    fov--;
                    break;
                }// end case
                case "R".charCodeAt():
                {
                    fov++;
                    break;
                }// end case
                case "G".charCodeAt():
                {
                    near = near - 10;
                    break;
                }// end case
                case "T".charCodeAt():
                {
                    near = near + 10;
                    break;
                }// end case
                case "H".charCodeAt():
                {
                    far = far - 10;
                    break;
                }// end case
                case "Y".charCodeAt():
                {
                    far = far + 10;
                    break;
                }// end case
                default:
                {
                    break;
                }// end default
            }// end switch
            return;
        }// end function

        public function set inertia(param1:Number) : void
        {
            this._inertia = param1;
            return;
        }// end function

        protected function setupEvents() : void
        {
            this.viewportStage = this.viewport3D.containerSprite.stage;
            this.viewportStage.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHandler);
            this.viewportStage.addEventListener(MouseEvent.MOUSE_UP, this.mouseUpHandler);
            this.viewportStage.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDownHandler);
            this.viewportStage.addEventListener(KeyboardEvent.KEY_UP, this.keyUpHandler);
            this.viewportStage.addEventListener(Event.ENTER_FRAME, this.onEnterFrameHandler);
            return;
        }// end function

        protected function displayProperties() : void
        {
            this._propertiesDisplay = new Sprite();
            this._propertiesDisplay.graphics.beginFill(0);
            this._propertiesDisplay.graphics.drawRect(0, 0, 100, 100);
            this._propertiesDisplay.graphics.endFill();
            this._propertiesDisplay.x = 0;
            this._propertiesDisplay.y = 0;
            var _loc_1:* = new TextFormat("_sans", 9);
            this.xText = new TextField();
            this.yText = new TextField();
            this.zText = new TextField();
            this.rotationXText = new TextField();
            this.rotationYText = new TextField();
            this.rotationZText = new TextField();
            this.fovText = new TextField();
            this.nearText = new TextField();
            this.farText = new TextField();
            var _loc_2:Array;
            var _loc_3:int;
            var _loc_4:Number;
            while (_loc_4++ < _loc_2.length)
            {
                // label
                _loc_2[_loc_4].width = 100;
                _loc_2[_loc_4].selectable = false;
                _loc_2[_loc_4].textColor = 16776960;
                _loc_2[_loc_4].text = "";
                _loc_2[_loc_4].defaultTextFormat = _loc_1;
                _loc_2[_loc_4].y = _loc_3 * _loc_4;
                this._propertiesDisplay.addChild(_loc_2[_loc_4]);
            }// end while
            this.viewport3D.addChild(this._propertiesDisplay);
            return;
        }// end function

        protected function onEnterFrameHandler(param1:Event) : void
        {
            if (this.keyForward)
            {
                this.forwardFactor = this.forwardFactor + 50;
            }// end if
            if (this.keyBackward)
            {
                this.forwardFactor = this.forwardFactor + -50;
            }// end if
            if (this.keyLeft)
            {
                this.sideFactor = this.sideFactor + -50;
            }// end if
            if (this.keyRight)
            {
                this.sideFactor = this.sideFactor + 50;
            }// end if
            var _loc_2:* = this.rotationX + (this.targetRotationX - this.rotationX) / this._inertia;
            var _loc_3:* = this.rotationY + (this.targetRotationY - this.rotationY) / this._inertia;
            this.rotationX = Math.round(_loc_2 * 10) / 10;
            this.rotationY = Math.round(_loc_3 * 10) / 10;
            this.forwardFactor = this.forwardFactor + -this.forwardFactor / this._inertia;
            this.sideFactor = this.sideFactor + -this.sideFactor / this._inertia;
            if (this.forwardFactor > 0)
            {
                this.moveForward(this.forwardFactor);
            }
            else
            {
                this.moveBackward(-this.forwardFactor);
            }// end else if
            if (this.sideFactor > 0)
            {
                this.moveRight(this.sideFactor);
            }
            else
            {
                this.moveLeft(-this.sideFactor);
            }// end else if
            this.xText.text = "x:" + int(x);
            this.yText.text = "y:" + int(y);
            this.zText.text = "z:" + int(z);
            this.rotationXText.text = "rotationX:" + int(_loc_2);
            this.rotationYText.text = "rotationY:" + int(_loc_3);
            this.rotationZText.text = "rotationZ:" + int(rotationZ);
            this.fovText.text = "fov:" + Math.round(fov);
            this.nearText.text = "near:" + Math.round(near);
            this.farText.text = "far:" + Math.round(far);
            return;
        }// end function

        protected function mouseUpHandler(param1:MouseEvent) : void
        {
            this.viewportStage.removeEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
            return;
        }// end function

        protected function keyUpHandler(param1:KeyboardEvent) : void
        {
            switch(param1.keyCode)
            {
                case "W".charCodeAt():
                case Keyboard.UP:
                {
                    this.keyForward = false;
                    break;
                }// end case
                case "S".charCodeAt():
                case Keyboard.DOWN:
                {
                    this.keyBackward = false;
                    break;
                }// end case
                case "A".charCodeAt():
                case Keyboard.LEFT:
                {
                    this.keyLeft = false;
                    break;
                }// end case
                case "D".charCodeAt():
                case Keyboard.RIGHT:
                {
                    this.keyRight = false;
                    break;
                }// end case
                default:
                {
                    break;
                }// end default
            }// end switch
            return;
        }// end function

        public function get propsDisplay() : Sprite
        {
            return this._propertiesDisplay;
        }// end function

        public function get inertia() : Number
        {
            return this._inertia;
        }// end function

        protected function onAddedToStageHandler(param1:Event) : void
        {
            this.setupEvents();
            return;
        }// end function

        protected function mouseMoveHandler(param1:MouseEvent) : void
        {
            this.targetRotationY = this.startRotationY - (this.startPoint.x - this.viewportStage.mouseX) / 2;
            this.targetRotationX = this.startRotationX + (this.startPoint.y - this.viewportStage.mouseY) / 2;
            return;
        }// end function

        protected function mouseDownHandler(param1:MouseEvent) : void
        {
            this.viewportStage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
            this.startPoint = new Point(this.viewportStage.mouseX, this.viewportStage.mouseY);
            this.startRotationY = this.rotationY;
            this.startRotationX = this.rotationX;
            return;
        }// end function

        public function set propsDisplay(param1:Sprite) : void
        {
            this._propertiesDisplay = param1;
            return;
        }// end function

        private function checkStageReady() : void
        {
            if (this.viewport3D.containerSprite.stage == null)
            {
                this.viewport3D.containerSprite.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            else
            {
                this.setupEvents();
            }// end else if
            return;
        }// end function

    }
}
